package com.example.iitematicviewer.api.author;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.iitematicviewer.constant.Constant;
import com.example.iitematicviewer.constant.ExceptionConstant;
import com.example.iitematicviewer.mapper.AuthorDao;
import com.example.iitematicviewer.mapper.CustomDao;
import com.example.iitematicviewer.mapper.ErrorDao;
import com.example.iitematicviewer.util.ErrorService;
import com.example.iitematicviewer.vo.AuthorVO;
import com.example.iitematicviewer.vo.CustomVO;
import com.example.iitematicviewer.vo.ErrorVO;
import com.example.iitematicviewer.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthorService {

    @Autowired
    private AuthorTransService service;

    @Autowired
    private AuthorDao dao;

    @Autowired
    private ErrorService errorService;

    @Autowired
    private CustomDao customDao;

    private ErrorVO errorVO;

    /**
     * 查询作者表(单查模糊+全查)
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO selectAuthor(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try {
            if (null != param){
                //作者名称
                String authorName = param.getString(AuthorConstant.authorName);
                //单个查询
                if (StringUtils.isNotEmpty(authorName)) {
                    List<AuthorVO> authorVOS = dao.selectOneByAuthorName(authorName);
                    if (null != authorVOS){
                        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(authorVOS));
                        resultVO.setDataList(jsonArray);
                        resultVO.setMessage(ExceptionConstant.select);
                    }
                }
            }
            //全查
            else {
                List<AuthorVO> authorVOS = dao.selectAll();
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(authorVOS));
                resultVO.setDataList(jsonArray);
                resultVO.setMessage(ExceptionConstant.select);
            }
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            if (null == e.getMessage()){
                resultVO.setMessage(ExceptionConstant.badSelect + ExceptionConstant.nullError);
            }
            else {
                resultVO.setMessage(ExceptionConstant.badSelect + e.getMessage());
            }
            if (null != param){
                errorVO = errorService.errorVO(resultVO,AuthorConstant.errorType,AuthorConstant.errorUrlByDelete,param.toString());
                errorService.errorinsert(errorVO);
            }
            else {
                errorVO = errorService.errorVO(resultVO,AuthorConstant.errorType,AuthorConstant.errorUrlByDelete,null);
                errorService.errorinsert(errorVO);
            }
        }
        return resultVO;
    }

    /**
     * 作者表删除方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO delete(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try {
            resultVO = judgingEmptiness(param, resultVO);
            if (resultVO.getCode().equals(ExceptionConstant.five)) {
                CustomVO customVO = customDao.selectOneByCode(AuthorConstant.deleteByAuthorUrl);
                ErrorVO errorVO = errorService.errorVO(resultVO, AuthorConstant.errorType, customVO.getContent(), param.toString());
                if (null != errorVO){
                    //存入失败方法
                    errorService.errorinsert(errorVO);
                }
                return resultVO;
            }
            //删除方法
            String id = param.getString(Constant.id);
            if (StringUtils.isEmpty(id)) {
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage("删除失败，未获取到主键信息");
                CustomVO customVO = customDao.selectOneByCode(AuthorConstant.deleteByAuthorUrl);
                ErrorVO errorVO = errorService.errorVO(resultVO, AuthorConstant.errorType, customVO.getContent(), param.toString());
                if (null != errorVO){
                    //存入失败方法
                    errorService.errorinsert(errorVO);
                }
                return resultVO;
            }
            //查询删除信息是否存在表中
            AuthorVO authorVO = dao.selectOneById(id);
            if (null == authorVO){
                resultVO.setCode(ExceptionConstant.five);
                resultVO.setMessage(ExceptionConstant.badDelete + AuthorConstant.notAuthor);
                CustomVO customVO = customDao.selectOneByCode(AuthorConstant.deleteByAuthorUrl);
                ErrorVO errorVO = errorService.errorVO(resultVO, AuthorConstant.errorType, customVO.getContent(), param.toString());
                if (null != errorVO){
                    //存入失败方法
                    errorService.errorinsert(errorVO);
                }
                return resultVO;
            }
            dao.delete(id);
            resultVO.setMessage(ExceptionConstant.delete);
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badDelete + e.getMessage());
        }
        CustomVO customVO = customDao.selectOneByCode(AuthorConstant.deleteByAuthorUrl);
        ErrorVO errorVO = errorService.errorVO(resultVO, AuthorConstant.errorType, customVO.getContent(), param.toString());
        if (null != errorVO){
            //存入成功
            errorService.errorinsert(errorVO);
        }
        return resultVO;
    }

    /**
     * 作者修改方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO update(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try{
            //判空方法
            resultVO = judgingEmptiness(param, resultVO);
            if (resultVO.getCode().equals(ExceptionConstant.five)) {
                return resultVO;
            }
            //先查询
            AuthorVO authorVO = dao.selectOneById(param.getString(Constant.id));
            if (null != authorVO){
                //修改方法
                authorVO = service.transJsonByUpdate(param, authorVO);
                dao.update(authorVO);
                resultVO.setMessage("修改成功");
            }
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage("修改失败" + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 作者新增方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO save(JSONObject param,ResultVO resultVO){
        resultVO.setCode(ExceptionConstant.two);
        try {
            //判空方法
            resultVO = judgingEmptiness(param,resultVO);
            if (resultVO.getCode().equals(ExceptionConstant.five)) {
                return resultVO;
            }
            //新增方法
            AuthorVO authorVO = service.transJsonBySave(param);
            dao.insert(authorVO);
            resultVO.setMessage(ExceptionConstant.insert);
        }
        catch (Exception e){
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badInsert + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 判空方法
     * @param param
     * @param resultVO
     * @return
     */
    public ResultVO judgingEmptiness(JSONObject param,ResultVO resultVO){
        //名称
        String authorName = param.getString(AuthorConstant.authorName);
        if (StringUtils.isEmpty(authorName)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badInsert + ExceptionConstant.mustname);
            return resultVO;
        }
        //个人链接
        String spacelink = param.getString(AuthorConstant.spacelink);
        if (StringUtils.isEmpty(spacelink)) {
            resultVO.setCode(ExceptionConstant.five);
            resultVO.setMessage(ExceptionConstant.badInsert + ExceptionConstant.MUSTSPACELINK);
            return resultVO;
        }
        return resultVO;
    }
}
